package cn.zct.domain.activity.service.discount.impl;

import cn.zct.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.zct.domain.activity.service.discount.AbstractDiscountCalculateService;
import cn.zct.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service("MJ")
public class MJCalculateService extends AbstractDiscountCalculateService {
    @Override
    protected BigDecimal doCalculate(BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {

        log.info("优惠策略折扣计算:{}", groupBuyDiscount.getDiscountType().getCode());

        String[] marketExpr = groupBuyDiscount.getMarketExpr().split(Constants.SPLIT);

        BigDecimal conditionCount = new BigDecimal(marketExpr[0].trim());
        BigDecimal subtractCount = new BigDecimal(marketExpr[1].trim());


        if (originalPrice.compareTo(conditionCount) < 0) {

            return originalPrice;

        }
        BigDecimal deductionPrice = originalPrice.subtract(subtractCount);

        if (deductionPrice.compareTo(BigDecimal.ZERO) <= 0) {
            return new BigDecimal("0.01");
        }

        return deductionPrice;
    }
}
